<?php
/**
 * 角色管理类
 * @author 肖小平
 *
 */
class RoleAction extends BaseAction{
	//展示角色列表
    public function role_list(){
    	$role_arr = M('AdminRole')->select();
    	$this->assign('role_arr',$role_arr);
		$this->display();//输出模板
    }
    //添加角色
    public function role_add(){
    	if($_POST){
    		M('AdminRole')->add($_POST);
    		$this->success('添加成功','role_list');
    		exit;
    	}
    	$this->display();//输出模板
    }
    //修改角色
    public function role_update(){
    	if($_POST){
    		M('AdminRole')->save($_POST);
    		$this->success('修改成功','role_list');
    		exit;
    	}
    	$role = M('AdminRole')->where('id = ' .$_GET['id'])->find();
    	$this->assign('role',$role);
    	$this->display();//输出模板
    }
    public function update_permission(){
    	if($_POST){
    		$data = array();
    		$data['menu_ids'] = implode(',', $_POST['menu_ids']);
    		$data['id'] = $_POST['id'];
    		M('AdminRole')->save($data);
    		$this->success("修改成功",'role_list');
    		exit;
    	}
    	$id = $_GET['id'];
    	$obj = M('AdminRole')->where("id = $id")->find();
    	$menus = M('AdminMenu')->select();
    	$menu_arr_list = array();
    	//获取第一级菜单
    	foreach ($menus as $m) {
    		if ($m['p_id']==0){
    			$menu_arr_list[] = $m;
    		}
    	}
    	//获取二级菜单
    	foreach ($menu_arr_list as $k=>$m_arr){
    		foreach ($menus as $m) {
    			if ($m['p_id']==$m_arr['id']){
    				$menu_arr_list[$k]['sub_menu'][] = $m;
    			}
    		}
    	}
    	$this->assign('menu_arr_list',$menu_arr_list);
    	$this->assign('obj',$obj);
    	$this->display();
    }
    //删除角色
    public function role_delete(){
    	M('AdminRole')->where('id = '.$_GET['id'])->delete();
    	$this->success('删除成功','role_list');
    }

}